package com.example.administrator.appstpremvpdemo.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.appstpremvpdemo.R;
import com.example.administrator.appstpremvpdemo.adapter.myViewpagerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/12/7.
 */
public class Gamefragent extends BaseFragment {

    private TabLayout mTabLayout;
    private ViewPager mViewpager;
    private TabLayout.Tab mTab1;
    private TabLayout.Tab mTab2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentview = inflater.inflate(R.layout.fragent3, container, false);
        initThisView(contentview);
        return contentview;
    }

    private void initThisView(View view) {
        mTabLayout = ((TabLayout) view.findViewById(R.id.tablayout));
        mViewpager = ((ViewPager) view.findViewById(R.id.viewpager));

        mTab1 = mTabLayout.newTab();
        mTab2 = mTabLayout.newTab();
        mTab1.setText("最新");
        mTab2.setText("热门");
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.holo_blue_bright));
        mTabLayout.addTab(mTab1, true);
        mTabLayout.addTab(mTab2, false);

        List<Fragment> list = new ArrayList<>();
        list.add(new TempFragment());
        list.add(new TempFragment());

        mViewpager.setAdapter(new myViewpagerAdapter(getActivity().getSupportFragmentManager(),list));

        mViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
